import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:ty_smart_watch/bean/logout_bean.dart';
import 'package:ty_smart_watch/common/common.dart';
import 'package:ty_smart_watch/route/routes.dart';
import 'package:ty_smart_watch/utils/Log.dart';
import 'package:ty_smart_watch/utils/http_util.dart';
import 'package:dio/dio.dart' as Resp;

class Logout extends StatelessWidget {
  const Logout({super.key});

  @override
  Widget build(BuildContext context) {
    return Center(
      child: RawMaterialButton(
        splashColor: Colors.transparent,
        highlightColor: Colors.transparent,
        onPressed: () => _logout(),
        // color: Colors.blue,
        // shape: const RoundedRectangleBorder(
        //     borderRadius: BorderRadius.all(Radius.circular(40))),
        child: Container(
          padding: const EdgeInsets.fromLTRB(80, 12, 80, 12),
          decoration: const BoxDecoration(
            //圆角半径
            borderRadius: BorderRadius.all(Radius.circular(50.0)),
            //边框线宽、颜色
            // border: Border.all(width: 1.0, color: Colors.blue),
            // 线性渐变，分别设置渐变的颜色，起始点
            gradient: LinearGradient(
                begin: Alignment.topCenter,
                end: Alignment.bottomCenter,
                colors: [Colors.purple, Colors.pinkAccent]),
          ),
          child: const Text(
            "退出登录",
            style: TextStyle(color: Colors.white),
          ),
        ),
      ),
    );
  }

  _logout() async {
    Log.d("退出登录");
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String token = prefs.getString(SP_ACCESS_TOKEN) ?? "";
    print("token ：$token");
    var dio = HttpUtil.instance?.serviceDio();
    dio.options.headers['tenant-id'] = "1";
    dio.options.headers['Authorization'] = token;
    Resp.Response response = await dio.post("/member/auth/logout");
    Log.d("退出登录结果： $response ");
    //删除本地信息
    prefs.remove(SP_USER_ID);
    prefs.remove(SP_ACCESS_TOKEN);
    prefs.remove(SP_REFRESH_TOKEN);
    Get.offAllNamed(Routes.loginPage);
  }
}
